
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;
var defineProperties = Object.defineProperties;
var DeveloperError = Cesium.DeveloperError;
var Event = Cesium.Event;
var createMaterialPropertyDescriptor = Cesium.createMaterialPropertyDescriptor;
var createPropertyDescriptor = Cesium.createPropertyDescriptor;

function ComsatScanGraphics(options) {
    this._show = undefined;
    this._angle = undefined;
    this._color = undefined;
    this._height = undefined;
    this._gaze = undefined;
    this._bottomArray = undefined;
    this._modelMatrix = undefined;
    this._position = undefined;
    this._scene = undefined;
    this._definitionChanged = new Event();
    this.merge(defaultValue(options, defaultValue.EMPTY_OBJECT));
}

Object.defineProperties(ComsatScanGraphics.prototype, {
    definitionChanged: {
        get: function () {
            return this._definitionChanged;
        }
    },
    show: createPropertyDescriptor('show'),
    height: createPropertyDescriptor('height'),
    angle: createPropertyDescriptor('angle'),
    color: createPropertyDescriptor('color'),
    gaze: createPropertyDescriptor('gaze'),
    bottomArray: createPropertyDescriptor('bottomArray'),
    modelMatrix: createPropertyDescriptor('modelMatrix'),
    position: createPropertyDescriptor('position'),
    scene: createPropertyDescriptor('scene'),

});

ComsatScanGraphics.prototype.clone = function (result) {
    if (!defined(result)) {
        result = new ComsatScanGraphics();
    }
    result.show = this.show;
    result.height = this.height;
    result.angle = this.angle;
    result.color = this.color;
    result.gaze = this.gaze;
    result.bottomArray = this.bottomArray;
    result.modelMatrix = this.modelMatrix;
    result.position = this.position;
    result.scene = this.scene;
    return result;
};

ComsatScanGraphics.prototype.merge = function (source) {
    if (!defined(source)) {
        throw new DeveloperError('source is required.');
    }

    this.show = defaultValue(this.show, source.show);
    this.height = defaultValue(this.height, source.height);
    this.angle = defaultValue(this.angle, source.angle);
    this.color = defaultValue(this.color, source.color);
    this.gaze = defaultValue(this.gaze, source.gaze);
    this.bottomArray = defaultValue(this.bottomArray, source.bottomArray);
    this.modelMatrix = defaultValue(this.modelMatrix, source.modelMatrix);
    this.position = defaultValue(this.position, source.position);
    this.scene = defaultValue(this.scene, source.scene);
};

ComsatScanGraphics.prototype.getValue = function () {
    // console.log(this.bottomArray);
    if (this.bottomArray && this.modelMatrix) {
        var _arr = [];
        for (let i = 0; i < this.bottomArray.length; i++) {
            const element = this.bottomArray[i];
            var point = renderPoint(element, this.position, this.modelMatrix, this.scene.globe.ellipsoid);
            point && _arr.push(point);
        }
        if (_arr[0]) {
            _arr.push(_arr[0].clone());
        }
        return _arr;
    }
}


var renderPoint = function (element, position, modelMatrix, ellipsoid) {
    let _localPos = new Cesium.Cartesian3(element[0], element[1], element[2]);
    let _worldPointX = Cesium.Matrix4.multiplyByPoint(modelMatrix, _localPos, new Cesium.Cartesian3());
    let result = new Cesium.Cartesian3();
    let direction = Cesium.Cartesian3.normalize(Cesium.Cartesian3.subtract(_worldPointX, position, result), result);
    var _ray = new Cesium.Ray(position, direction)
    var intersection = Cesium.IntersectionTests.rayEllipsoid(_ray, ellipsoid);
    if (intersection) {
        var p = Cesium.Ray.getPoint(_ray, intersection.start);
        return p
    } else {
        return undefined;
    }
}

export { ComsatScanGraphics };